#ifndef __STATUS_SERVER_H__
#define __STATUS_SERVER_H__

#include "../protos/message.grpc.pb.h"
#include "../protos/message.pb.h"

#include <cstdint>
#include <unordered_map>
#include <vector>
#include <map>
#include <mutex>
#include <grpcpp/grpcpp.h>


using grpc::Channel;        // gRPC 通道
using grpc::ClientContext;  // 客户端上下文
using grpc::Status;         // 状态
using grpc::ServerContext;  // 服务器上下文
using message::GetChatServerReq; // 请求消息
using message::GetChatServerRsp; // 响应消息
using message::StatusService;    // gRPC 服务
using message::LoginReq;         // 登录请求
using message::LoginRsp;         // 登录响应



struct ChatServer 
{
    std::string port;
    std::string host;
    std::string name;
    uint32_t cur_load; // 当前负载
};

class StatusServerImpl final : public StatusService::Service
{
public:
    StatusServerImpl();

    // 获取聊天服务器信息
    Status GetChatServer(ServerContext* context, const GetChatServerReq* request,
        GetChatServerRsp* reply) override;

    // 用户登录
    Status Login(ServerContext* context, const LoginReq* request,
        LoginRsp* reply) override;

private:
    void _InsertToken(uint32_t uid, const std::string& token);

    ChatServer _GetChatServer();

private:
    std::unordered_map<std::string, ChatServer> _server_map; // 服务器列表
    std::unordered_map<uint32_t, std::string> _tokens; // token 列表
    std::mutex _token_mutex;
    std::mutex _server_mutex;
};


#endif // !__STATUS_SERVER_H__